package complatefuturedemo;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author chaird
 * @create 2023-05-20 20:23
 */
public class App {
  public static void main(String[] args) throws Exception {

    // 版本2
    long start = System.currentTimeMillis();
    App app = new App();
    Object creative = null;

    CompletableFuture<String> t1 =
        CompletableFuture.supplyAsync(
            () -> {
              return app.m1(creative);
            });

    CompletableFuture<String> t2 =
        CompletableFuture.supplyAsync(
            () -> {
              return app.m2(creative);
            });

    CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(t1, t2);

    try { TimeUnit.SECONDS.sleep(20); } catch (Exception e) { e.printStackTrace(); } finally { }
    System.out.println("睡完");


    System.out.println((System.currentTimeMillis() - start) / 1000);

    // 版本1
    //      long start = System.currentTimeMillis();
    //      App app = new App();
    //      Object creative = null;
    //      String s = app.m1(creative);
    //      if (null != s){
    //      System.out.println("校验失败1：" + s);
    //      return;
    //      }
    //      s = app.m2(creative);
    //      if (null != s){
    //          System.out.println("校验失败2：" + s);
    //          return;
    //      }
    //
    //    System.out.println((System.currentTimeMillis() - start) / 1000);
  }

  public String m1(Object creative) {
    // 规则1
    System.out.println("规则1 in");
    sleepMethod(3);
    System.out.println("规则1 out");
    return null;
  }

  public String m2(Object creative) {
    // 规则2
    System.out.println("规则2 in");
    sleepMethod(3);
    int i = 1/0;
    System.out.println("规则2 out");
    return null;
  }

  public void sleepMethod(Integer num) {

    try {
      TimeUnit.SECONDS.sleep(num);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
    }
  }
}
